package com.hxjc.common.utils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * FileLoader
 *
 * @author yinz
 * @date 2018/5/11
 */
public class FileLoader {

    static byte[] loadBytes(String path, Charset encoding) throws IOException {
        return Files.readAllBytes(Paths.get(path));
    }

    static String readFile(String path, Charset encoding)
            throws IOException
    {
        return new String(loadBytes(path, encoding), encoding);
    }

    public static byte[] readUTF8FileToBytes(String path) {
        Charset charset = Charset.forName("UTF-8");
        byte[] file = new byte[0];
        try {
            file = loadBytes(path, charset);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static String readUTF8File(String path) {
        Charset charset = Charset.forName("UTF-8");
        String file = "";
        try {
            file = readFile(path, charset);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }
}
